package cn.imvc.app.controllers.goods;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import cn.imvc.app.entities.Brand;
import cn.imvc.app.models.BrandModel;
import cn.imvc.app.models.LoggingModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;
/**
 * 
 * @author zhy
 * @date 2016年7月22日下午2:22:21
 *
 */
public class BrandControllerImpl implements BrandController {
	static Logger logger = Logger.getLogger(BrandControllerImpl.class);
	public Page<Brand> page;
	public ViewRequest viewRequest;
	public Brand brand;
	public int id;
	public int[] ids;
	public String brandTree;	
	private String search_content;
	
	/**
	 * 多选的查找带回
	 * @return
	 */
	private String multipleDialogMyIds;
	private String multipleDialogMyNames;
	@Autowired
    BrandModel brandModel;
	@Autowired
	LoggingModel loggingModel;
    
	public String search() {
		
		return "brand/search";
	}

	public String index() {
		if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		viewRequest.getSort().put("sort", "ASC");
		if(search_content != null && !search_content.trim().equals("")){
			viewRequest.getParams().put("code_like", search_content);
			viewRequest.getParams().put("_or_name_like", search_content);
			viewRequest.getParams().put("_or_sort_like", search_content);
		}
		page = brandModel.find(viewRequest);
		return "brand/index";
	}

	public String show() {
		brand = brandModel.get(id);
		return "brand/edit";
	}

	public String editNew() {
		
		return "brand/edit";
	}

	public String create() {
		int result = brandModel.save(brand);
		if(result==0){
			return Render.json(new DialogBean("300","品牌编码不能重复","brand","closeCurrent"));
		}
		if(result==-1){
			return Render.json(new DialogBean("300","品牌名称不能重复","brand","closeCurrent"));
		}
		loggingModel.info(logger,"添加品牌："+brand.getName());
		    return Render.json(new DialogBean("200","创建成功","","closeCurrent"));
	}

	public String edit() {
		brand = brandModel.get(id);
		return "brand/edit";
	}

	public String update() {
		int result = brandModel.update(id,brand);
		if(result==0){
			return Render.json(new DialogBean("300","品牌编码不能重复","brand","closeCurrent"));
		}
		if(result==-1){
			return Render.json(new DialogBean("300","品牌名称不能重复","brand","closeCurrent"));
		}
		loggingModel.info(logger,"修改品牌："+brand.getName());
		return Render.json(new DialogBean("200","修改成功","","closeCurrent"));
	}
    
	public String destroy() {
		String result = brandModel.delete(id);
		if(result.equals("")){
			loggingModel.info(logger,"删除品牌："+id);
			return Render.json(new DialogBean("200","删除成功",""));
		}else{
			return Render.json(new DialogBean("300", result,"")); 
		}
	}

	public String destroyBatch() {
		brandModel.deleteBatch(ids);
		return Render.json(new DialogBean("200","删除成功",""));
	}
	
    public String dialog(){
    	if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		viewRequest.getSort().put("sort", "ASC");
		if(search_content != null && !search_content.trim().equals("")){
			viewRequest.getParams().put("code_like", search_content);
			viewRequest.getParams().put("_or_name_like", search_content);
			viewRequest.getParams().put("_or_sort_like", search_content);
		}
		page = brandModel.find(viewRequest);
    	return "brand/dialog";
    }
    
    /**
     * 多选的dialog
     */
    public String multipleDialog(){
    	if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		viewRequest.getSort().put("sort", "ASC");
		if(search_content != null && !search_content.trim().equals("")){
			viewRequest.getParams().put("code_like", search_content);
			viewRequest.getParams().put("_or_name_like", search_content);
			viewRequest.getParams().put("_or_sort_like", search_content);
		}
    	page = brandModel.find(viewRequest);
    	return "brand/multipleDialog";
    }

	public String getMultipleDialogMyIds() {
		return multipleDialogMyIds;
	}

	public void setMultipleDialogMyIds(String multipleDialogMyIds) {
		this.multipleDialogMyIds = multipleDialogMyIds;
	}

	public String getMultipleDialogMyNames() {
		return multipleDialogMyNames;
	}

	public void setMultipleDialogMyNames(String multipleDialogMyNames) {
		this.multipleDialogMyNames = multipleDialogMyNames;
	}
	public ViewRequest getViewRequest() {
		return viewRequest;
	}

	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public Brand getBrand() {
		return brand;
	}

	public void setBrand(Brand brand) {
		this.brand = brand;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int[] getIds() {
		return ids;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}
	
	public String getSearch_content() {
		return search_content;
	}

	public void setSearch_content(String search_content) {
		this.search_content = search_content;
	}
}
